using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace System.Utility.Helper
{
	public static class Images
	{
		public static ImageSource CreateImageSourceFromImage(Bitmap image)
		{
			ImageSource imageSource;
			if (image != null)
			{
				try
				{
					IntPtr hbitmap = image.GetHbitmap();
					BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
					bitmapSource.Freeze();
					image.Dispose();
					Win32.Win32.DeleteObject(hbitmap);
					imageSource = bitmapSource;
				}
				catch (Exception exception)
				{
					imageSource = null;
				}
			}
			else
			{
				imageSource = null;
			}
			return imageSource;
		}

		public static ImageSource CreateImageSourceThumbnia(string fileName, double width, double height)
		{
			Image image = Image.FromFile(fileName);
			double num = width / (double)image.Width;
			double num1 = height / (double)image.Height;
			float single = (float)Math.Min(num, num1);
			int num2 = image.Width;
			int num3 = image.Height;
			if (single < 1f)
			{
				num2 = (int)Math.Round((double)((float)image.Width * single));
				num3 = (int)Math.Round((double)((float)image.Height * single));
			}
			Bitmap bitmap = new Bitmap(image, num2, num3);
			IntPtr hbitmap = bitmap.GetHbitmap();
			BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
			bitmapSource.Freeze();
			Win32.Win32.DeleteObject(hbitmap);
			image.Dispose();
			bitmap.Dispose();
			return bitmapSource;
		}

		public static ImageSource CreateImageSourceThumbnia(Image sourceImage, double width, double height)
		{
			ImageSource imageSource;
			if (sourceImage != null)
			{
				double num = width / (double)sourceImage.Width;
				double num1 = height / (double)sourceImage.Height;
				float single = (float)Math.Min(num, num1);
				int num2 = sourceImage.Width;
				int num3 = sourceImage.Height;
				if (single < 1f)
				{
					num2 = (int)Math.Round((double)((float)sourceImage.Width * single));
					num3 = (int)Math.Round((double)((float)sourceImage.Height * single));
				}
				Bitmap bitmap = new Bitmap(sourceImage, num2, num3);
				IntPtr hbitmap = bitmap.GetHbitmap();
				BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
				bitmapSource.Freeze();
				Win32.Win32.DeleteObject(hbitmap);
				sourceImage.Dispose();
				bitmap.Dispose();
				imageSource = bitmapSource;
			}
			else
			{
				imageSource = null;
			}
			return imageSource;
		}

		public static ImageSource CreateImageSourceThumbnia(byte[] data, double width, double height)
		{
			ImageSource imageSource;
			Stream memoryStream = new MemoryStream(data, true);
			try
			{
				Image image = Image.FromStream(memoryStream);
				try
				{
					imageSource = CreateImageSourceThumbnia(image, width, height);
				}
				finally
				{
					if (image != null)
					{
						((IDisposable)image).Dispose();
					}
				}
			}
			finally
			{
				if (memoryStream != null)
				{
					((IDisposable)memoryStream).Dispose();
				}
			}
			return imageSource;
		}

		public static Image CreateThumbnail(Stream fileStream, int width, int height, Drawing.Color penColor)
		{
			Bitmap bitmap;
			try
			{
				bitmap = new Bitmap(fileStream);
			}
			catch
			{
				bitmap = new Bitmap(width, height);
			}
			return CreateThumbnail(bitmap, width, height, penColor);
		}

		public static Image CreateThumbnail(string fileName, int width, int height, Drawing.Color penColor)
		{
			Bitmap bitmap;
			try
			{
				bitmap = new Bitmap(fileName);
			}
			catch
			{
				bitmap = new Bitmap(width, height);
			}
			return CreateThumbnail(bitmap, width, height, penColor);
		}

		public static Image CreateThumbnail(Image image, int width, int height, Drawing.Color penColor)
		{
			Bitmap bitmap;
			try
			{
				bitmap = new Bitmap(image);
			}
			catch
			{
				bitmap = new Bitmap(width, height);
			}
			return CreateThumbnail(bitmap, width, height, penColor);
		}

		public static Image CreateThumbnail(Bitmap bitmap, int width, int height, Drawing.Color penColor)
		{
			Image image;
			width = (bitmap.Width > width ? width : bitmap.Width);
			height = (bitmap.Height > height ? height : bitmap.Height);
			Bitmap bitmap1 = new Bitmap(width, height, Drawing.Imaging.PixelFormat.Format64bppPArgb);
			Graphics graphic = Graphics.FromImage(bitmap1);
			try
			{
				int num = width;
				int num1 = height;
				if (bitmap.Width > bitmap.Height)
				{
					num1 = (int)((float)bitmap.Height / (float)bitmap.Width * (float)num);
				}
				else if (bitmap.Width < bitmap.Height)
				{
					num = (int)((float)bitmap.Width / (float)bitmap.Height * (float)num1);
				}
				int num2 = width / 2 - num / 2;
				int num3 = height / 2 - num1 / 2;
				graphic.PixelOffsetMode = PixelOffsetMode.None;
				graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
				graphic.DrawImage(bitmap, num2, num3, num, num1);
				Drawing.Pen pen = new Drawing.Pen(penColor, 1f);
				try
				{
					graphic.DrawRectangle(pen, 0, 0, bitmap1.Width - 1, bitmap1.Height - 1);
				}
				finally
				{
					if (pen != null)
					{
						((IDisposable)pen).Dispose();
					}
				}
				image = bitmap1;
			}
			finally
			{
				if (graphic != null)
				{
					((IDisposable)graphic).Dispose();
				}
			}
			return image;
		}

		public static void DrawImage(Graphics g, Rectangle rect, Image img, float opacity)
		{
			if (opacity > 0f)
			{
				ImageAttributes imageAttribute = new ImageAttributes();
				try
				{
					SetImageOpacity(imageAttribute, (opacity >= 1f ? 1f : opacity));
					int x = rect.X;
					int y = rect.Y + rect.Height / 2;
					Drawing.Size size = img.Size;
					int height = y - size.Height / 2;
					int width = img.Size.Width;
					size = img.Size;
					Rectangle rectangle = new Rectangle(x, height, width, size.Height);
					g.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttribute);
				}
				finally
				{
					if (imageAttribute != null)
					{
						((IDisposable)imageAttribute).Dispose();
					}
				}
			}
		}

		public static void SetImageOpacity(ImageAttributes imgAttributes, float opacity)
		{
			float[][] singleArray = new float[5][];
			float[] singleArray1 = new float[] { 1f, default(float), default(float), default(float), default(float) };
			singleArray[0] = singleArray1;
			singleArray1 = new float[] { default(float), 1f, default(float), default(float), default(float) };
			singleArray[1] = singleArray1;
			singleArray1 = new float[] { default(float), default(float), 1f, default(float), default(float) };
			singleArray[2] = singleArray1;
			singleArray1 = new float[] { default(float), default(float), default(float), opacity, default(float) };
			singleArray[3] = singleArray1;
			singleArray1 = new float[] { default(float), default(float), default(float), default(float), 1f };
			singleArray[4] = singleArray1;
			imgAttributes.SetColorMatrix(new ColorMatrix(singleArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
		}
	}
}